class Solution {
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};
    int m = 0;
    int n = 0;
    int change;
    int val = 0;

    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        if(image[sr][sc] == color){
            return image;
        }
        m = image.length;
        n = image[0].length;
        change = color;
        val = image[sr][sc];

        image[sr][sc] = color;
        dfs(image, sr, sc);
        return image;
    }
    public void dfs(int[][] image, int sr, int sc){
        for(int i = 0; i < 4; i++){
            int x = sr + dx[i];
            int y = sc + dy[i];
            if(x >= 0 && x < m && y >= 0 && y < n && image[x][y] == val){
                image[x][y] = change;
                dfs(image, x, y);
            }
        }
    }
}